﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    public class RegInfoDAL
    {
        private string DbName = "LdtCommonManage";

        /// <summary>
        /// 获取RegexInfoUnit数据
        /// </summary>
        /// <param name="eiId"></param>
        /// <returns></returns>
        public DataTable GetRegexInfoUnit(string eiId)
        {
            string strsql = string.Format("select * from Reg_InfoUnit where EI_ID in ({0})", eiId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            var ds = oBase.ExecuteDataSet(CommandType.Text, strsql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            { return ds.Tables[0]; }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取Reg_InfoRegexRelation数据
        /// </summary>
        /// <param name="eiId"></param>
        /// <returns></returns>
        public DataTable Reg_InfoRegexRelation(string eiId)
        {
            string strsql = string.Format("select * from Reg_InfoRegexRelation where EI_ID in ({0})", eiId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            var ds = oBase.ExecuteDataSet(CommandType.Text, strsql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            { return ds.Tables[0]; }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取Reg_Regexs数据
        /// </summary>
        /// <param name="eiId"></param>
        /// <returns></returns>
        public DataTable GetReg_Regexs(string eiId)
        {
            string strsql = string.Format("select * from Reg_Regexs where ER_ID in(select ER_ID from Reg_InfoRegexRelation where EI_ID in ({0}))", eiId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            var ds = oBase.ExecuteDataSet(CommandType.Text, strsql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            { return ds.Tables[0]; }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取Reg_InfoUnit表结构
        /// </summary>
        /// <returns></returns>
        public DataTable GetRegexInfoUnitTableStructs()
        {
            string strsql = "select * from Reg_InfoUnit where 1<>1";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            var ds = oBase.ExecuteDataSet(CommandType.Text, strsql);
            return ds.Tables[0];
        }


        /// <summary>
        /// 获取Reg_InfoRegexRelation表结构
        /// </summary>
        /// <returns></returns>
        public DataTable GeReg_InfoRegexRelationTableStructs()
        {
            string strsql = "select * from Reg_InfoRegexRelation where 1<>1";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            var ds = oBase.ExecuteDataSet(CommandType.Text, strsql);
            return ds.Tables[0];
        }

        /// <summary>
        /// 获取Reg_Regexs表结构
        /// </summary>
        /// <returns></returns>
        public DataTable GeReg_RegexsTableStructs()
        {
            string strsql = "select * from Reg_Regexs where 1<>1";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            var ds = oBase.ExecuteDataSet(CommandType.Text, strsql);
            return ds.Tables[0];
        }


        /// <summary>
        ///根据CCID修改信息单元的名称
        /// </summary>
        /// <returns></returns>
        public bool UpdateEIName(string oldName,string newName,string ccId)
        {        
            string strsql =string.Format(" update LdtCommonManage.dbo.Reg_InfoUnit set  EI_Name = replace(EI_Name, '{0}', '{1}') where EI_Code in(select CC_RegexInfoCode from Ldt_JudgeDocSystem.dbo.SYS_Express_Source where  CC_ID = '{2}' )", oldName, newName, ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            int  ds = Convert.ToInt32(oBase.ExecuteNonQuery(CommandType.Text, strsql));
            return ds > 0 ? true : false;
        }
    }
}
